package com.abigdreamer.im.server.ui.desktop.internal;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JDesktopPane;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;


public class EIMWindow extends JDesktopPane implements MouseListener {

	private JPopupMenu pop = null; // 弹出菜单
	private JMenuItem newItem = null, property = null, refresh = null; // 三个功能菜单
	
	int arranges=6;
	int itemRightExcursion=170;
	int itemDownExcursion=50;
	int itemWeigth=170;
	int itemHigth=32;
	
	public EIMWindow(){
		this.setBackground(Color.WHITE);
		this.addMouseListener(this);
		creatPopupMenu();
	}
	
	private void creatPopupMenu() {
	    pop = new JPopupMenu();
		pop.add(newItem = new JMenuItem("新建"));
		pop.add(new JSeparator());
		pop.add(refresh = new JMenuItem("刷新"));
		pop.add(new JSeparator());
		pop.add(property = new JMenuItem("属性"));

		newItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuAction(e);
			}
		});
		refresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuAction(e);
			}
		});

		property.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuAction(e);
			}
		});
	}

	public void add(WindowItem item,int num){
		
		int row=0;
		int arry=num%(arranges);
		if(arry==0){
			row=num/(arranges);
			arry=arranges;
		}else{
			row=num/(arranges)+1;
		}
		int x=(row-1)*itemRightExcursion;
		int y=(arry-1)*itemDownExcursion;
		item.setBounds(x, y, itemWeigth, itemHigth); 
		this.add(item);
		
	}
	public void menuAction(ActionEvent e) {
		String str = e.getActionCommand();
		if (str.equals(newItem.getText())) {
		} else if (str.equals(refresh.getText())) {
			this.repaint();
		} else if (str.equals(property.getText())) {
		}
	}

	public void mouseClicked(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON3) {
			pop.show(this, e.getX(), e.getY());
		}
	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {

	}

	public void mouseReleased(MouseEvent e) {

	}

}
